package com.mb.app.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.dao.TbShipFeeCfgMapper;
import com.mb.app.domain.SysAreaCoord;
import com.mb.app.domain.TbShipFeeCfg;
import com.mb.app.service.ISysAreaCoordService;
import com.mb.app.service.ITbShipFeeCfgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运费配置(模板)  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-04-10
 */
@Service
@Slf4j
public class TbShipFeeCfgServiceImpl extends ServiceImpl<TbShipFeeCfgMapper, TbShipFeeCfg> implements ITbShipFeeCfgService {

    @Autowired
    private ISysAreaCoordService sysAreaCoordService;

    @Override
    public List<TbShipFeeCfg> queryList(Map<String, Object> map) {
        List<TbShipFeeCfg> list = this.baseMapper.lst(map);
        List<TbShipFeeCfg> tbProductShipFeeCfgs1 = new ArrayList<TbShipFeeCfg>();
        if(list.size()>0) {
            for(int i = 0; i < list.size();i++) {
                TbShipFeeCfg shipFeeCfg = list.get(i);
                if(shipFeeCfg != null && shipFeeCfg.getAreaIds() != null && shipFeeCfg.getAreaIds() != "") {
                    String[] areaIds = shipFeeCfg.getAreaIds().split(",");
                    String sysName = "";
                    String sysIds = "";
                    for (int j = 0; j < areaIds.length; j++){
                        SysAreaCoord sysAreaCoord = sysAreaCoordService.selectById(areaIds[j]);
                        if (sysAreaCoord != null) {
                            sysName = sysName + "," + sysAreaCoord.getName();
                            sysIds = sysIds + "," + sysAreaCoord.getId();
                        }
                    }
                    if (sysName != "" && sysIds != "") {
                        sysName = sysName.substring(1);
                        sysIds = sysIds.substring(1);
                    }
                    shipFeeCfg.setAreaIds(sysIds);
                    shipFeeCfg.setAreaNames(sysName);
                }
                tbProductShipFeeCfgs1.add(shipFeeCfg);
            }
        }
        return tbProductShipFeeCfgs1;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public int save(TbShipFeeCfg TbShipFeeCfg) {
        TbShipFeeCfg.setCreateTime(new Date());
        return this.baseMapper.insert(TbShipFeeCfg);
    }

    @Override
    public int update(TbShipFeeCfg TbShipFeeCfg) {
        try {
            TbShipFeeCfg TbShipFeeCfg1 = new TbShipFeeCfg();
            TbShipFeeCfg1.setId(TbShipFeeCfg.getId());
            TbShipFeeCfg1.setDataStatus(1);
            TbShipFeeCfg TbShipFeeCfgDO = this.baseMapper.selectOne(TbShipFeeCfg1);
            if(TbShipFeeCfgDO != null){
                TbShipFeeCfg.setUpdateTime(new Date());
                this.baseMapper.updateById(TbShipFeeCfg);
            }
        } catch (Exception e) {
            log.error("update Exception",e);
        }
        return 0;
    }


    @Override
    public int remove(Long Id,Long delUserId) {
//        return this.baseMapper.deleteById(appId);
        TbShipFeeCfg TbShipFeeCfg = new TbShipFeeCfg();
        TbShipFeeCfg.setId(Id);
        TbShipFeeCfg.setDeleteTime(new Date());
        TbShipFeeCfg.setDataStatus(0);
        TbShipFeeCfg.setDeleteUserId(delUserId);
        return this.baseMapper.updateById(TbShipFeeCfg);
    }

    @Override
    public TbShipFeeCfg getDtl(Long Id) {
        TbShipFeeCfg tbShipFeeCfg = new TbShipFeeCfg();
        tbShipFeeCfg.setId(Id);
        tbShipFeeCfg.setDataStatus(1);
        TbShipFeeCfg TbShipFeeCfg = this.baseMapper.selectOne(tbShipFeeCfg);
        if(TbShipFeeCfg != null && TbShipFeeCfg.getAreaIds() != null) {
            String[] areaIds = TbShipFeeCfg.getAreaIds().split(",");
            String sysName = "";
            String sysIds = "";
            for (int i = 0; i < areaIds.length; i++){
                SysAreaCoord sysAreaCoord = sysAreaCoordService.selectById(areaIds[i]);
                if (sysAreaCoord != null) {
                    sysName = sysName + "," + sysAreaCoord.getName();
                    sysIds = sysIds + "," + sysAreaCoord.getId();
                }
            }
            if (sysName != "" && sysIds != "") {
                sysName = sysName.substring(1);
                sysIds = sysIds.substring(1);
            }
            TbShipFeeCfg.setAreaNames(sysName);
            TbShipFeeCfg.setAreaIds(sysIds);
        }
        return TbShipFeeCfg;
    }
}
